package com.example.carrierapp.ui.main.dispatch.search

import android.app.Activity
import android.content.Context

import com.example.carrierapp.base.BasePresenter
import com.example.carrierapp.bean.dispatch.CarFreeBean
import com.example.carrierapp.bean.dispatch.SearchDriverBean
import com.example.carrierapp.net.ApiMiddleHelper
import com.example.carrierapp.net.listener.DataCallBackListener
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.viewmodel.itemmodel.ItemSearchModel
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken

import org.json.JSONException
import org.json.JSONObject

import java.util.HashMap

import io.reactivex.Flowable

class DispatchSearchPresenter(private val context: Activity) : BasePresenter<DispatchSearchContract.View>(), DispatchSearchContract.Presenter {

    override fun start() {

    }

    override fun getDriverList(filter: String) {
        val biddingMap = HashMap<String, String>()
        biddingMap["key"] = filter
        biddingMap["maxResultCount"] = "30"
        biddingMap["skipCount"] = "0"
        val flowable = apiStrores.searchDriver(biddingMap)

        ApiMiddleHelper.getInstance().toNetIo2Main(context, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("getDriverList-onSuccess-$json")
                val bean = Gson().fromJson<MutableList<ItemSearchModel>>(json, object : TypeToken<MutableList<ItemSearchModel>>() {}.type)
                view?.onSearchSucess(bean)
            }

            override fun onFailure(msg: String) {
                LogUtil.d("getDriverList-onFailure-$msg")
                if (view != null) {
                    view.onSearchError(msg)
                }
            }
        })
    }

    override fun getCarNumList(filter: String) {
        val biddingMap = HashMap<String, String>()
        biddingMap["key"] = filter
        biddingMap["maxResultCount"] = "30"
        biddingMap["skipCount"] = "0"
        val flowable = apiStrores.getAllCarPages(biddingMap)

        ApiMiddleHelper.getInstance().toNetIo2Main(context, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("getAllCarPages-onSuccess-$json")
                val bean = Gson().fromJson<MutableList<ItemSearchModel>>(json, object : TypeToken<MutableList<ItemSearchModel>>() {}.type)
                if (view != null) {
                    view.onSearchSucess(bean)
                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("getAllCarPages-onFailure-$msg")
                if (view != null) {
                    view.onSearchError(msg)

                }
            }
        })
    }
}
